/*
*	This is the ALU in execute beat.
*	Done in 2011.4.5
*	funcode :
*		000 -> A AND B
*		001 -> A OR B
*		010 -> A + B
*		011 -> A - B
*		100 -> ~(A OR B)
*		101 -> A << B
*		110 -> A >> B
*		111 -> SLt
*		
*/

module ALU(
	SrcAD,
	SrcBD,
	ALUControl,
	ALUOut
);
	input[31:0] SrcAD,SrcBD;
	input[2:0] ALUControl;
	output[31:0] ALUOut;
	reg[31:0] ALUOut;
	
	//initial
	initial begin
		ALUOut = 0;
	end
	
	always @(SrcAD or SrcBD or ALUControl) begin
		case(ALUControl)
			3'b000:
				ALUOut = SrcAD & SrcBD;
			3'b001:
				ALUOut = SrcAD | SrcBD;
			3'b010:
				ALUOut = SrcAD + SrcBD;
			3'b011:
				ALUOut = SrcAD - SrcBD;
			3'b100:
				ALUOut = ~(SrcAD | SrcBD);
			3'b101:
				ALUOut = SrcAD << SrcBD;
			3'b110:
				ALUOut = SrcAD >> SrcBD;
			3'b111:
				if(SrcAD[31] == 0 && SrcBD[31] == 0) begin
					if(SrcAD < SrcBD) ALUOut = 1;
					else ALUOut = 0;
				end
				else if(SrcAD[31] == 1 && SrcBD[31] == 0) ALUOut = 1;
				else if(SrcAD[31] == 0 && SrcBD[31] == 1) ALUOut = 0;
				else if(SrcAD[31] == 1 && SrcBD[31] == 1) begin
					if(-SrcAD > -SrcBD) ALUOut = 1;
					else ALUOut = 0;
				end
			default:
				ALUOut = 32'bz;
		endcase
	end
endmodule
